//
//  YXYNextTableViewCell.m
//  Topit
//
//  Created by dllo on 15/12/4.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYNextTableViewCell.h"
#import "UIImageView+WebCache.h"
@implementation YXYNextTableViewCell

-(void)dealloc {
    [_name release];
    [_content release];
    [_cnum release];
    [_fnum release];
    [_ts release];
    [_images release];
    [_idName release];
    [_idImage release];
    [_nextModel release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.name = [[UILabel alloc] initWithFrame:CGRectZero];
        _name.font = [UIFont systemFontOfSize:15];
        _name.numberOfLines = 2;
        [self addSubview:_name];
        [_name release];
        
        self.content = [[UILabel alloc] initWithFrame:CGRectZero];
        _content.numberOfLines = 0;
        _content.font = [UIFont systemFontOfSize:10];
        [self addSubview:_content];
        [_content release];
        
        self.cnum = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_cnum];
        [_cnum release];
        
        self.fnum = [[UILabel alloc] initWithFrame:CGRectZero];
        _fnum.font = [UIFont systemFontOfSize:8];
        _fnum.textColor = [UIColor grayColor];
        [self addSubview:_fnum];
        [_fnum release];
        
        self.ts = [[UILabel alloc] initWithFrame:CGRectZero];
        _ts.font = [UIFont systemFontOfSize:8];
        _ts.textColor = [UIColor grayColor];
        [self addSubview:_ts];
        [_ts release];
        
        self.images = [[UIImageView alloc] initWithFrame:CGRectZero];
        _images.contentMode = UIViewContentModeScaleAspectFill;
        _images.clipsToBounds = YES;
        [self addSubview:_images];
        [_images release];
        
        self.idImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        _idImage.layer.cornerRadius = 15;
        _idImage.layer.masksToBounds = YES;
        [self addSubview:_idImage];
        [_idImage release];
        
        self.idName = [[UILabel alloc] initWithFrame:CGRectZero];
        _idName.font = [UIFont systemFontOfSize:11];
        [self addSubview:_idName];
        [_idName release];


    }
    return self;
}

-(void)setNextModel:(NextModel *)nextModel {
    if (_nextModel != nextModel) {
        [_nextModel release];
        _nextModel = [nextModel retain];
    }
    _name.text = nextModel.name;
    _content.text = nextModel.content;
    [_images sd_setImageWithURL:[NSURL URLWithString:[nextModel.items lastObject][@"icon"][@"url"]]];
    _cnum.text = nextModel.cnum;
    _fnum.text = nextModel.fnum;
    _fnum.text = [_fnum.text stringByAppendingString:@"回复"];
    _ts.text = nextModel.ts;
    _ts.text = [_ts.text stringByAppendingString:@" / 共"];
    [_idImage sd_setImageWithURL:[NSURL URLWithString:nextModel.user[@"icon"][@"url"]]];
    _idName.text = nextModel.user[@"name"];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _idImage.frame = CGRectMake(5, 5, 30, 30);
    CGFloat kOriginX = _idImage.frame.origin.x + _idImage.frame.size.width + 10;
    _idName.frame = CGRectMake(kOriginX, _idImage.frame.origin.y + 3, self.frame.size.width, 10);
    _ts.frame = CGRectMake(kOriginX, _idName.frame.origin.y + 15, 40, 10);
    _fnum.frame = CGRectMake(kOriginX + _ts.frame.size.width, _ts.frame.origin.y, 50, 10);
    _name.frame = CGRectMake(5, _idImage.frame.size.height + 10, self.frame.size.width, 40);
    
    //    通过文字计算label的size
    
    //    定义一个字典存储lebel上的字体等信息
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:10]};
    CGRect rect = [_content.text boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGFloat kOriginy = _name.frame.origin.y + _name.frame.size.height + 5;
    _content.frame = CGRectMake(5, kOriginy, self.frame.size.width - 5, rect.size.height);
    _images.frame = CGRectMake(2, _content.frame.size.height + kOriginy, self.frame.size.width, 240);


}

- (CGFloat)getHeight {
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:10]};
    CGRect rect = [_content.text boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
//    rect.size.height = _images.frame.size.height;
//    NSLog(@"🍎%f", _images.frame.size.height);
    return rect.size.height + _images.frame.size.height + 90;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
